package com.yunhe.merchants.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.merchants.model.DeptPlan;
import com.yunhe.merchants.model.SalesmanPlan;
import com.yunhe.merchants.model.Statistics;
import com.yunhe.merchants.service.IPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PlanController
 * @Description:
 * @author: GongDaCheng
 * @Date: 15:02 2021/4/28
 */
@Api(value = "planManage", description = "计划管理", tags = {"Model_PLAN_MANAGE"})
@RestController
@RequestMapping("/planManage")
public class PlanController {
    @Autowired
    IPlanService planService;

    @ApiOperation("制定部门计划")
    @PostMapping(value = "/makeDeptPlan")
    public ResponseEntity<ResultObject> makeDeptPlan(@RequestBody List<DeptPlan> deptPlans) {
        int count = planService.makeDeptPlan(deptPlans);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }

    @ApiOperation("查询部门计划")
    @GetMapping(value = "/getDeptPlan")
    public ResponseEntity<ResultObject> getDeptPlan(@RequestParam(value = "year") String year) throws ArgumentErrorException {
        List<DeptPlan> deptPlans = planService.getDeptPlan(year);
        return new ResponseEntity<>(new ResultObject<>(deptPlans), HttpStatus.OK);
    }

    @ApiOperation("部门计划统计")
    @GetMapping(value = "/deptPlanStatistics")
    public ResponseEntity<ResultObject> deptPlanStatistics(@RequestParam(value = "deptId") Long deptId,
                                                           @RequestParam(value = "type") String type,
                                                           @RequestParam(value = "year", required = true) String year,
                                                           @RequestParam(value = "quarter", required = false) String quarter,
                                                           @RequestParam(value = "month", required = false) String month) {
        if ("0".equals(type)) {
            month = null;
            quarter = null;
        } else if ("1".equals(type)) {
            month = null;
        }
        if ("2".equals(type)) {
            quarter = null;
        }
        Statistics statistics = planService.deptPlanStatistics(Arrays.asList(deptId), type, year, quarter, month);
        return new ResponseEntity<>(new ResultObject<>(statistics), HttpStatus.OK);
    }

    @ApiOperation("制定业务员计划")
    @PostMapping(value = "/makeSalesmanPlan")
    public ResponseEntity<ResultObject> makeSalesmanPlan(@RequestBody List<SalesmanPlan> salesmanPlans) throws ArgumentErrorException {
        int count = planService.makeSalesmanPlan(salesmanPlans);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }

    @ApiOperation("查询业务员计划")
    @GetMapping(value = "/getSalesmanPlan")
    public ResponseEntity<ResultObject> getSalesmanPlan(@RequestParam(value = "year") String year) throws ArgumentErrorException {
        List<SalesmanPlan> salesmanPlans = planService.getSalesmanPlan(year);
        return new ResponseEntity<>(new ResultObject<>(salesmanPlans), HttpStatus.OK);
    }

    @ApiOperation("业务员计划统计")
    @GetMapping(value = "/salesmanPlanStatistics")
    public ResponseEntity<ResultObject> salesmanPlanStatistics(@RequestParam(value = "deptId") Long deptId,
                                                               @RequestParam(value = "salesmanId") Long salesmanId,
                                                               @RequestParam(value = "type") String type,
                                                               @RequestParam(value = "year", required = true) String year,
                                                               @RequestParam(value = "quarter", required = false) String quarter,
                                                               @RequestParam(value = "month", required = false) String month) {
        if ("0".equals(type)) {
            month = null;
            quarter = null;
        } else if ("1".equals(type)) {
            month = null;
        }
        if ("2".equals(type)) {
            quarter = null;
        }
        Statistics statistics = planService.salesmanPlanStatistics(deptId, salesmanId, type, year, quarter, month);
        return new ResponseEntity<>(new ResultObject<>(statistics), HttpStatus.OK);
    }


    @ApiOperation("园区计划统计")
    @GetMapping(value = "/parkPlanStatistics")
    public ResponseEntity<ResultObject> parkPlanStatistics(@RequestParam(value = "type") String type,
                                                           @RequestParam(value = "year") String year,
                                                           @RequestParam(value = "quarter", required = false) String quarter,
                                                           @RequestParam(value = "month", required = false) String month) throws ArgumentErrorException {
        if ("0".equals(type)) {
            month = null;
            quarter = null;
        } else if ("1".equals(type)) {
            month = null;
        }
        if ("2".equals(type)) {
            quarter = null;
        }
        Statistics statistics = planService.parkPlanStatistics(type, year, quarter, month);
        return new ResponseEntity<>(new ResultObject<>(statistics), HttpStatus.OK);
    }
}
